﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Web.Wap.Code;
using DotNet.Common.Command;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;
using Newtonsoft.Json;

namespace ZLCMS.Web.Wap.Shop
{
    public partial class GetScore : MemberPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.RequestType == "POST")
            {
                var result = new CommandResult<object>() { ErrorCode = "0001", Result = "请根据规定调用接口" };

                if (Session[GlobalSessionString.UserCheckCode] as string != Request["CheckCode"])
                {
                    result.ErrorMsg = "输入的验证码错误";
                }
                else
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        var ticket = session.CreateQuery("from ShopCouponTicket where TicketCode=:code and Status!=9")
                            .SetParameter("code", Request["Code"])
                            .UniqueResult<ShopCouponTicket>();

                        if (ticket != null)
                        {
                            int count = session.CreateSQLQuery("update member_info set coins=coins+:coins where account_id=:acc and(coins + :coins) >= 0")
                                 .SetParameter("coins", ticket.Amount)
                                 .SetParameter("acc", CurrentMemberAccount)
                                 .ExecuteUpdate();
                            int currentScore = CurrentMember.Coins + ticket.Amount;
                            if (count > 0)
                            {
                                session.Save(new MemberScoreLog()
                                {
                                    MemberAccount = CurrentMemberAccount,
                                    Score = ticket.Amount,
                                    ScoreType = "score",
                                    Remark = "兑换积分券" + ticket.TicketCode + "获取积分" + ticket.Amount,
                                    FinalScore = currentScore,
                                    OwnerAccount = "admin"
                                });
                                ticket.Status = "9";
                                ticket.MemberAccount = CurrentMemberAccount;
                                ticket.UsedDate = DateTime.Now;
                                session.Update(ticket);
                                session.Flush();

                                result.ErrorCode = "0000";
                                result.ErrorMsg = "操作成功";
                                result.Result = count;
                            }
                            else
                            {
                                result.ErrorCode = "0001";
                                result.ErrorMsg = "添加积分失败";
                            }

                        }
                        else
                        {
                            result.ErrorCode = "0001";
                            result.ErrorMsg = "该兑换码已失效";
                        }
                    }
                }

                Response.Write(JsonConvert.SerializeObject(result));
                Response.End();
            }
        }


    }
}